<?php

namespace App\Model;

use EasySwoole\Mysqli\Exception\Exception;
use EasySwoole\ORM\AbstractModel;

/**
 * Class CouponsGroupLogModel
 * Create With Automatic Generator
 * @property $id
 * @property $user_id
 * @property $coupons_id
 * @property $create_time
 * @property $update_time
 * @property $validate_time
 * @property $status
 * @property $used_time
 * @property $recovery_time
 * @property $consume_id
 */
class CouponsUserModel extends AbstractModel
{
    protected $tableName = 'bee_coupons_user';

    protected $primaryKey = 'id';

    protected $autoTimeStamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    /**
     * 获取用户单张优惠券信息通过自增ID
     * @param int $couponsId
     * @return CouponsUserModel|array|bool|AbstractModel|\EasySwoole\ORM\Db\Cursor|\EasySwoole\ORM\Db\CursorInterface|null
     * @throws Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function findCouponsById(int $couponsId) {
        $field = [
            'cu.*',
            'c.name',
            'c.target_money',
            'c.offer_money',
            'c.is_condition',
            'c.btype_id',
            'c.stype_id',
            'ct.name as type_name',
        ];
        $this->field($field);
        $this->where('cu.id', $couponsId);
        $this->alias('cu');
        $this->join('coupons c', 'cu.coupons_id = c.id');
        $this->join('coupons_type ct', 'ct.id = c.type');
        return $this->get();
    }
}